import sys
import json
import urllib, json, urllib2, hashlib,struct,sha,time
import socket
import smtplib  
from email.mime.text import MIMEText  
from email.header import Header  
import time

bh_market_url = "https://api.bithumb.com/public/"
zb_market_url = "http://api.zb.com/data/v1/"
hb_market_url = "https://api.hitbtc.com/api/2/public/ticker/"
access_key = '00000000000000000000000000000'
access_secret = '333333333333333333333333333'
KRW_RMB = 1 / 164.0727


kraken_market_url = "https://api.kraken.com/0/public/"
USD_RMB = 6.5274 / 0.99

def getDifferenceRateBHKraken():
    zb_price = {'btc_qc': 0, 'eth_qc': 0, 'ltc_qc': 0, 'eos_qc': 0, 'qtum_qc': 0, 'etc_qc': 0,'xrp_qc':0}
    zb_ticker = zb_market_url + "ticker?market=" 
   
    kr_ticker = kraken_market_url + "Ticker?pair="
    
    btc_zb = 0
    req = urllib2.Request(zb_ticker + 'btc_qc')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker) 
    btc_zb = round(float(dicTicker['ticker']['last']),2)

    eth_zb = 0
    req = urllib2.Request(zb_ticker + 'eth_qc')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    eth_zb = round(float(dicTicker['ticker']['last']),2)

    etc_zb = 0
    req = urllib2.Request(zb_ticker + 'etc_qc')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    etc_zb = round(float(dicTicker['ticker']['last']),2)

    eos_zb = 0
    req = urllib2.Request(zb_ticker + 'eos_qc')
    res = urllib2.urlopen(req)
    ticker = res.read()   
    dicTicker = json.loads(ticker)
    eos_zb = round(float(dicTicker['ticker']['last']),2)

    xrp_zb = 0
    req = urllib2.Request(zb_ticker + 'xrp_qc')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    xrp_zb = round(float(dicTicker['ticker']['last']),2)

    btc_kr = 0
    req = urllib2.Request(kr_ticker + 'XBTUSD')
    res = urllib2.urlopen(req)
    ticker = res.read()   
    dicTicker = json.loads(ticker)
    btc_kr = round(float(dicTicker['result']['XXBTZUSD']['c'][0]) * USD_RMB,2)

    eth_kr = 0
    req = urllib2.Request(kr_ticker + 'ETHUSD')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    eth_kr = round(float(dicTicker['result']['XETHZUSD']['c'][0]) * USD_RMB,2)
 
    etc_kr = 0
    req = urllib2.Request(kr_ticker + 'ETCUSD')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    etc_kr = round(float(dicTicker['result']['XETCZUSD']['c'][0]) * USD_RMB,2)

    eos_kr = 0
    req = urllib2.Request(kr_ticker + 'EOSETH')
    res = urllib2.urlopen(req)
    ticker = res.read()   
    dicTicker = json.loads(ticker)
    eos_kr = round(float(dicTicker['result']['EOSETH']['c'][0]) * eth_kr,2)
  
    xrp_kr = 0
    req = urllib2.Request(kr_ticker + 'XRPUSD')
    res = urllib2.urlopen(req)
    ticker = res.read()
    dicTicker = json.loads(ticker)
    xrp_kr = round(float(dicTicker['result']['XXRPZUSD']['c'][0]) * USD_RMB,2)
    
    result = {}
    btc_meta = {} 
    btc_meta['rate'] = round((btc_zb - btc_kr) / btc_kr, 4)
    btc_meta['zb'] = btc_zb
    btc_meta['kr'] = btc_kr
    result['btc']= btc_meta  

    eth_meta = {}
    eth_meta['rate'] = round((eth_zb - eth_kr) / eth_kr, 4)
    eth_meta['zb'] = eth_zb
    eth_meta['kr'] = eth_kr
    result['eth'] = eth_meta

    etc_meta = {}
    etc_meta['rate'] = round((etc_zb - etc_kr) / etc_kr, 4)
    etc_meta['zb'] = etc_zb
    etc_meta['kr'] = etc_kr
    result['etc'] = etc_meta

    eos_meta = {}
    eos_meta['rate'] = round((eos_zb - eos_kr) / eos_kr, 4)
    eos_meta['zb'] = eos_zb
    eos_meta['kr'] = eos_kr
    result['eos'] = eos_meta
    
    xrp_meta = {}
    xrp_meta['rate'] = round((xrp_zb - xrp_kr) / xrp_kr, 4)
    xrp_meta['zb'] = xrp_zb
    xrp_meta['kr'] = xrp_kr
    result['xrp'] = xrp_meta
    return result

    

def convertBH2ZB(bhName):
    return bhName.lower() + "_qc"

def findChanceXMR_BH_Hitbtc():
    xmr_hb_ticker = hb_market_url + "XMRETH"
    xmr_bh_ticker = bh_market_url + 'ticker/' +"XMR"

    print xmr_bh_ticker

    req = urllib2.Request(xmr_bh_ticker)
    res = urllib2.urlopen(req)
    ticker = res.read()
    xmr_bh_price = json.loads(ticker)
    xmr_bh = round(float(xmr_bh_price['data']['buy_price']), 4)

    req = urllib2.Request(xmr_hb_ticker)
    res = urllib2.urlopen(req)
    ticker = res.read()
    xmr_hb_price = json.loads(ticker)
    xmr_hb = round(float(xmr_hb_price['ask']), 6)

    xmr_rmb = round(4.8 / xmr_hb * xmr_bh / 164, 2)

    result ={}
    result['bithumb'] = xmr_bh
    result['hitbtc']  = xmr_hb
    result['xmrrmb'] = xmr_rmb

    return result


def get_BH_ZB_Diff():

    zb_price = {'btc_qc': 0, 'eth_qc': 0, 'ltc_qc': 0, 'eos_qc': 0, 'qtum_qc': 0, 'etc_qc': 0,'xrp_qc':0}
    zb_ticker = zb_market_url + "ticker?market="

    bh_price = {'BTC': 0, 'ETH': 0, 'LTC': 0, 'EOS': 0, 'QTUM': 0, 'ETC': 0,'XRP':0}
    bh_ticker = bh_market_url + "ticker/"

    rate_bh_zb = {'BTC': 0, 'ETH': 0, 'LTC': 0, 'EOS': 0, 'QTUM': 0, 'ETC': 0}
    result = {}

# Get Price from ZB.com
    for key in zb_price:
        req = urllib2.Request(zb_ticker + key)
        res = urllib2.urlopen(req)
        ticker = res.read()
        dicTicker = json.loads(ticker)
        zb_price[key] = float(dicTicker['ticker']['last'])

    for key in bh_price:
        req = urllib2.Request(bh_ticker + key)
        res = urllib2.urlopen(req)
        ticker = res.read()
        dicTicker = json.loads(ticker)
        bh_price[key] = round(float(dicTicker['data']['buy_price']) * KRW_RMB, 2)

    for key in bh_price:
        rate_bh_zb[key] = round(((bh_price[key] - zb_price[convertBH2ZB(key)]) / zb_price[convertBH2ZB(key)]), 3)

    diff = {
        'big':{
            "pair":0,
            "value":0
        },
        'second':{
        "pair":0,
        "value":0
        }
    }

    exist_pair = {}
    for key in rate_bh_zb:
        for k in rate_bh_zb:
            if k is not key:
                pair_name = key + "_" + k
                pair_name1 = k + "_" + key
                if not (exist_pair.has_key(pair_name) or exist_pair.has_key(pair_name1)):
                    pair_value = round((rate_bh_zb[key] - rate_bh_zb[k]), 5)
                    exist_pair[pair_name] = pair_value
                    if abs(pair_value) > abs(diff['big']['value']):
                        diff['second']['pair'] = diff['big']['pair']
                        diff['second']['value'] = diff['big']['value']
                        diff['big']['value'] = pair_value
                        diff['big']['pair'] = pair_name
                    elif abs(pair_value) > abs(diff['second']['value']):
                        diff['second']['pair'] = pair_name
                        diff['second']['value'] = pair_value
    result['zb_price'] = zb_price
    result['bh_price'] = bh_price
    result['diff'] = diff
    result['rate_bh_zb'] = rate_bh_zb
    return result


def sendEmail(address, title, body):
    sender = 'bigbitcoin@163.com'
    receiver = address  
    subject = title  
    smtpserver = 'smtp.163.com'
    username = 'bigbitcoin@163.com'
    password = 'w20171226'
      
    msg = MIMEText(body,'plain','utf-8')  
    msg['Subject'] = Header(subject, 'utf-8')  
    msg['from'] = sender
    msg['to'] = receiver  
    smtp = smtplib.SMTP_SSL()
    smtp.connect('smtp.163.com')
    smtp.login(username, password)  
    smtp.sendmail(sender, receiver, msg.as_string())  
    smtp.quit()  

